﻿using System;
using System.Linq;
using System.Web.Mvc;
using OTE.Infrastructure.ConfirgurationEngine;
using OTE.Infrastructure.Extensions;
using OTE.Tasks.Commands.Category;
using OTE.Web.Infrastructure;
using OTE.Web.Infrastructure.Utils;
using OTE.Web.Mvc.Queries.Category;
using OTE.Web.Mvc.ViewModels.Category;

namespace OTE.Web.Mvc.Areas.Admin.Controllers
{
    public class CategoryController : OTEController
    {
        public ICategoryQuery CategoryQuery { get; private set; }

        public IConfigurator Configurator { get; private set; }

        public CategoryController(
            ICategoryQuery categoryListQuery,
            IConfigurator configurator)
        {
            CategoryQuery = categoryListQuery;
            Configurator = configurator;
        }

        #region list

        [HttpGet]
        public ActionResult Index()
        {
            ViewData[Constants.TOTAL_PAGE] = Configurator.GetAppSettingString("PageSize").ToInterger();

            ViewData[Constants.PAGE_INDEX] = Configurator.GetAppSettingString("PageIndex").ToInterger();

            var viewModel = new CategoryListViewModel { Categories = CategoryQuery.GetPagedList(1, 10) };

            return View(viewModel);
        }

        [HttpGet]
        public ActionResult Create()
        {
            var viewModel = new CreateCategoryViewModel();

            viewModel.ParentCategories = CategoryQuery.GetCategoryParent().ToList();

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult SaveCreated()
        {
            var viewModel = new CreateCategoryViewModel();

            TryUpdateModel(viewModel);

            var command = new CategoryAddedCommand
                              {
                                  ParentId = viewModel.Category.ParentId,
                                  ContentName = viewModel.Category.Name,
                                  CreatedBy = User != null ? User.Identity.Name : ""
                              };

            var result = CommandProcessor.Process(command);

            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var category = CategoryQuery.GetCategoryById(id);

            if (category == null)
                throw new Exception("Cannot get category from database");

            var parents = CategoryQuery.GetCategoryParent().ToList();

            var viewModel = new UpdateCategoryViewModel
                                {
                                    Category = category,
                                    ParentCategories = parents
                                };

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult SaveEdited()
        {
            var viewModel = new UpdateCategoryViewModel();

            TryUpdateModel(viewModel);

            var command = new CategoryUpdatedCommand
                              {
                                  CategoryId = viewModel.Category.Id,
                                  ParentId = viewModel.Category.ParentId,
                                  ContentName = viewModel.Category.Name
                              };

            var result = CommandProcessor.Process(command);

            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            var command = new CategoryDeletedCommand
                              {
                                  CategoryId = id
                              };

            var result = CommandProcessor.Process(command);

            return RedirectToAction("Index");
        }

        #endregion list
    }
}